const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardCur: 0,
    isVidoUrl: '',
    currentId:'',
    StartTime: '',
    EndTime: '',
    swiperList:null,

    descList:'',
    isdesc:'',
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //this.towerSwiper('swiperList');
    this.getDesc(options);
    // 初始化towerSwiper 传已有的数组名即可
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var timestamp = Date.parse(new Date()); //进入页面的时间戳
    var Stimestamp = timestamp / 1000;
    this.setData({
      StartTime: Stimestamp
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function (options) {
    var timestamp = Date.parse(new Date()); //离开页面的的时间戳
    var Etimestamp = timestamp / 1000;
    this.setData({
      EndTime: Etimestamp
    });
    app.core('post', 'create_data_statistics', {
      // data 部分
      'type':4,
      'visit_time': this.data.EndTime - this.data.StartTime,
      'read_id': this.data.currentId,
      'open_id': wx.getStorageSync('user').openid
    }, (res) => {
        console.log(res)
    },
      (err) => {
        console.log(err)
      }
    )

  },

  // 立即拨打
  goMobile (e) {
    var mobilenum = e.currentTarget.dataset.num;
    wx.makePhoneCall({
      phoneNumber: mobilenum
    })
  },
  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },

  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },
  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  // towerSwiper计算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },
  // towerSwiper计算滚动
  towerEnd(e) {
    let direction = this.data.direction;
    let list = this.data.swiperList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    }
  },

  //获取详情
  getDesc: function (options){
    var id = options.id;
    var vm = this;
    vm.setData({
      currentId:id
    });
    var url = "get_user_card_detail/id/"+id
    app.core('get', url, {
      // data 部分
    }, (res) => {
      //个人相册数组集合
      var imgsList = [];
      var imgs = res.data.album.replace(/\[|]/g, '').replace(/["]/g, "").replace(/[\\]/g, "");
      imgs = imgs.split(",");
      for(var k = 0; k < imgs.length; k++){
        imgsList.push({
          'id':k,
          'type':'image',
          'url': imgs[k]
        })
      }
      vm.setData({
        swiperList: imgsList
      })

      var resList = [];
      resList.push({
        name: res.data.name,
        address: res.data.address,
        tel: res.data.tel,
        mobile: res.data.mobile,
        email: res.data.email,
        logo: res.data.headpic
      });
    
      vm.setData({
        descList: resList,
        isVidoUrl: res.data.video,
        isdesc: res.data.desc == "" ? '暂无简介' : res.data.desc
      })
    },
      (err) => {
        console.log(err)
      }
    )

  }

})